package com.cy.model;

import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("serial")
@TableBind(tableName="device_sales", pkName = "id")
public class DeviceSales extends BasicModel<DeviceSales> {
    public static final DeviceSales dao = new DeviceSales();
    public static final String SELECT_ALL = "`id`,`user_id`,`sn`,`sales_time`,`memo`,`status`";

    @Override
    public Page<DeviceSales> paginate(Integer pageNumber, Integer pageSize, String select, DeviceSales model) {
        List<Object> paras = new ArrayList<>();
        StringBuilder sqlExceptSelectBuffer = new StringBuilder("FROM device_sales ds, user u WHERE u.user_id = ds.user_id");
        StringBuilder selectBuffer = new StringBuilder("SELECT ").append(select);
        if (pageNumber == null || pageNumber == 0) {
            pageNumber = 1;
        } else {
            pageNumber++;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }

        if (model != null) {
            String[] attrNames = model.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = model.get(attrName);
                if (attrValue != null) {
                    if("start".equals(attrName)) {
                        sqlExceptSelectBuffer.append(" AND `sales_time` >= ?");
                    } else if("end".equals(attrName)) {
                        sqlExceptSelectBuffer.append(" AND `sales_time` <= ?");
                    } else if("sn".equals(attrName)) {
                        sqlExceptSelectBuffer.append(" AND `sn` LIKE ?");
                        attrValue = "%"+attrValue+"%";
                    } else {
                        sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
                    }
                    paras.add(attrValue);
                }
            }
        }
        sqlExceptSelectBuffer.append(" ORDER BY `sales_time` DESC ");
        if (paras.size() > 0) {
            return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString(), paras.toArray());
        }
        return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString());
    }

    /**
     * 查询销售记录
     */
    public List<DeviceSales> findByParam(Long userId, String sn, String startDate, String endDate) {
        StringBuilder sql = new StringBuilder(50);
        sql.append("SELECT ds.*, IF(da.status = 1, 1, 0) AS isActivated ");
        sql.append("FROM device_sales ds LEFT JOIN device_activation da ON ds.sn = da.sn ");
        sql.append("WHERE 1 = 1 ");
        if (userId != null)
            sql.append("AND ds.user_id = "+ userId +" ");
        if (StringUtils.isNotBlank(sn))
            sql.append("AND ds.sn = '"+ sn +"' ");
        if (StringUtils.isNotBlank(startDate))
            sql.append("AND ds.sales_time >= '"+startDate+" 00:00:00' ");
        if (StringUtils.isNotBlank(endDate))
            sql.append("AND ds.sales_time <= '"+endDate+" 23:59:59' ");
        sql.append("ORDER BY ds.sales_time DESC");
        return find(sql.toString());
    }

    /**
     * 查询二次销售记录
     */
    public List<DeviceSales> findSecond(String sn, String startDate, String endDate) {
        StringBuilder sql = new StringBuilder(50);
        sql.append("SELECT ds.*, u.account FROM device_sales ds, user u WHERE ds.user_id = u.user_id AND sn = ? ");
        if (StringUtils.isNotBlank(startDate))
            sql.append("AND ds.sales_time > '"+startDate+"' ");
        if (StringUtils.isNotBlank(endDate))
            sql.append("AND ds.sales_time <= '"+endDate+"' ");
        sql.append("ORDER BY ds.sales_time DESC");
        return find(sql.toString(), sn);
    }

    /**
     * 查询最近销售记录
     */
    public DeviceSales findLastBySn(String sn) {
        return findFirst("SELECT * FROM device_sales WHERE sn = ? ORDER BY sales_time DESC LIMIT 1", sn);
    }

    public DeviceSales findByUserAndSn(Long userId, String sn) {
        return findFirst("SELECT * FROM device_sales WHERE user_id = ? AND sn = ? ORDER BY sales_time DESC LIMIT 1", userId, sn);
    }

    public DeviceSales setId(Long id){
        return this.set("id", id);
    }

    public Long getId(){
        return this.get("id");
    }
        
    public DeviceSales setUserId(Long userId){
        return this.set("user_id", userId);
    }

    public Long getUserId(){
        return this.get("user_id");
    }

    public DeviceSales setSn(String sn){
        return this.set("sn", sn);
    }

    public String getSn(){
        return this.get("sn");
    }

    public DeviceSales setSalesTime(Date salesTime){
        return this.set("sales_time",salesTime);
    }

    public Date getSalesTime(){
        return this.get("sales_time");
    }

    public DeviceSales setMemo(String memo){
        return this.set("memo",memo);
    }

    public String getMemo(){
        return this.getStr("memo");
    }

    public DeviceSales setStatus(Integer status){
        return this.set("status",status);
    }

    public String getStatus(){
        return this.getStr("status");
    }

}